View Javadoc
1 package org.apache.commons.betwixt.digester; 2 3 /* 4 * ==================================================================== 5 * 6 * The Apache Software License, Version 1.1 7 * 8 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 9 * reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in 20 * the documentation and/or other materials provided with the 21 * distribution. 22 * 23 * 3. The end-user documentation included with the redistribution, if 24 * any, must include the following acknowlegement: 25 * "This product includes software developed by the 26 * Apache Software Foundation (http://www.apache.org/)." 27 * Alternately, this acknowlegement may appear in the software itself, 28 * if and wherever such third-party acknowlegements normally appear. 29 * 30 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 31 * Foundation" must not be used to endorse or promote products derived 32 * from this software without prior written permission. For written 33 * permission, please contact apache@apache.org. 34 * 35 * 5. Products derived from this software may not be called "Apache" 36 * nor may "Apache" appear in their names without prior written 37 * permission of the Apache Group. 38 * 39 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 40 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 42 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 46 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 47 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 49 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This software consists of voluntary contributions made by many 54 * individuals on behalf of the Apache Software Foundation. For more 55 * information on the Apache Software Foundation, please see 56 * <http://www.apache.org/>;. 57 */ 58 import java.beans.BeanInfo; 59 import java.beans.Introspector; 60 import java.beans.PropertyDescriptor; 61 62 import org.apache.commons.betwixt.AttributeDescriptor; 63 import org.apache.commons.betwixt.ElementDescriptor; 64 import org.apache.commons.betwixt.expression.ConstantExpression; 65 66 import org.apache.commons.logging.Log; 67 import org.apache.commons.logging.LogFactory; 68 69 import org.xml.sax.Attributes; 70 import org.xml.sax.SAXException; 71 72 /*** 73 * <p><code>AttributeRule</code> the digester Rule for parsing the 74 * <attribute> elements.</p> 75 * 76 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 77 * @version $Id: AttributeRule.java,v 1.1 2002/06/10 17:53:35 jstrachan Exp $ 78 */ 79 public class AttributeRule extends RuleSupport { 80 81 /*** Logger */ 82 private static final Log log = LogFactory.getLog( AttributeRule.class ); 83 84 private ClassLoader classLoader; 85 86 private Class beanClass; 87 88 public AttributeRule() { 89 this.classLoader = getClass().getClassLoader(); 90 } 91 92 // Rule interface 93 //------------------------------------------------------------------------- 94 95 /*** 96 * Process the beginning of this element. 97 * 98 * @param attributes The attribute list of this element 99 */ 100 public void begin(Attributes attributes) throws Exception { 101 102 AttributeDescriptor descriptor = new AttributeDescriptor(); 103 String name = attributes.getValue( "name" ); 104 descriptor.setQualifiedName( name ); 105 descriptor.setLocalName( name ); 106 String uri = attributes.getValue( "uri" ); 107 if ( uri != null ) { 108 descriptor.setURI( uri ); 109 } 110 String propertyName = attributes.getValue( "property" ); 111 descriptor.setPropertyName( propertyName ); 112 descriptor.setPropertyType( loadClass( attributes.getValue( "type" ) ) ); 113 114 if ( propertyName != null && propertyName.length() > 0 ) { 115 configureDescriptor(descriptor); 116 } 117 else { 118 String value = attributes.getValue( "value" ); 119 if ( value != null ) { 120 descriptor.setTextExpression( new ConstantExpression( value ) ); 121 } 122 } 123 124 Object top = digester.peek(); 125 if ( top instanceof ElementDescriptor ) { 126 ElementDescriptor parent = (ElementDescriptor) top; 127 parent.addAttributeDescriptor( descriptor ); 128 } 129 else { 130 throw new SAXException( "Invalid use of <attribute>. It should " + 131 "be nested inside an <element> element" ); 132 } 133 134 digester.push(descriptor); 135 } 136 137 138 /*** 139 * Process the end of this element. 140 */ 141 public void end() throws Exception { 142 Object top = digester.pop(); 143 } 144 145 146 // Implementation methods 147 //------------------------------------------------------------------------- 148 protected Class loadClass( String name ) { 149 // XXX: should use a ClassLoader to handle complex class loading situations 150 if ( name != null ) { 151 try { 152 return classLoader.loadClass(name); 153 } 154 catch (Exception e) { 155 } 156 } 157 return null; 158 } 159 160 /*** Set the Expression and Updater from a bean property name */ 161 protected void configureDescriptor(AttributeDescriptor attributeDescriptor) { 162 Class beanClass = getBeanClass(); 163 if ( beanClass != null ) { 164 String name = attributeDescriptor.getPropertyName(); 165 try { 166 BeanInfo beanInfo = Introspector.getBeanInfo( beanClass ); 167 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); 168 if ( descriptors != null ) { 169 for ( int i = 0, size = descriptors.length; i < size; i++ ) { 170 PropertyDescriptor descriptor = descriptors[i]; 171 if ( name.equals( descriptor.getName() ) ) { 172 XMLIntrospectorHelper 173 .configureProperty( attributeDescriptor, descriptor ); 174 getProcessedPropertyNameSet().add( name ); 175 break; 176 } 177 } 178 } 179 } 180 catch (Exception e) { 181 log.warn( "Caught introspection exception", e ); 182 } 183 } 184 } 185 }

This page was automatically generated by Maven